package com.punai.customer.controller;

import cn.hutool.core.io.FileUtil;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.punai.common.annotation.Log;
import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.core.page.TableDataInfo;
import com.punai.common.enums.BusinessType;
import com.punai.common.properties.SqliteUpPathConfig;
import com.punai.customer.domain.CusImport;
import com.punai.customer.domain.vo.CusAllVo;
import com.punai.customer.domain.vo.CusImpVo;
import com.punai.customer.service.ICusImportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * CusImportController
 *
 * @author zxw
 * @date 2022-12-27
 */
@RestController
@RequestMapping("/cus/import")
public class CusImportController extends BaseController
{
    @Autowired
    private ICusImportService cusImportService;

    /**
     * 查询CusImport列表
     */
    @PreAuthorize("@ss.hasPermi('cus:import:list')")
    @GetMapping("/list")
    public TableDataInfo list(CusImport cusImport)
    {
        startPage();
        List<CusImport> list = cusImportService.selectCusImportList(cusImport);
        return getDataTable(list);
    }


    /**
     * 新增CusImport
     */
    @PreAuthorize("@ss.hasPermi('cus:import:add')")
    @Log(title = "数据导入", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CusImpVo vo)
    {
        return toAjax(cusImportService.insertCusImport(vo));
    }

    /**
     * 新增 材料章节 / 材料费用项
     */
    @PreAuthorize("@ss.hasPermi('cus:materialItem:customerImp')")
    @Log(title = "数据导入", businessType = BusinessType.INSERT)
    @PostMapping("material")
    public AjaxResult excelMaterialAdd(@RequestBody CusImpVo vo)
    {
        try{
            Map<String, Integer> stringIntegerMap = cusImportService.insertCusMaterialImport(vo);
            return AjaxResult.success(stringIntegerMap);
        }catch (ExcelAnalysisException e) {
            return AjaxResult.error("失败的excel解析!");
        }
    }


    /**
     * 新增 材料章节 / 材料费用项  从标准费用项复制
     */
    @PreAuthorize("@ss.hasPermi('cus:materialItem:useSta')")
    @Log(title = "数据导入", businessType = BusinessType.INSERT)
    @PostMapping("materialCopySta")
    public AjaxResult materialCopySta()
    {
        return toAjax(cusImportService.materialCopySta());
    }


    /**
     * 新增 机械章节 / 机械费用项
     */
    @PreAuthorize("@ss.hasPermi('cus:mechanicalItem:customerImp')")
    @Log(title = "数据导入", businessType = BusinessType.INSERT)
    @PostMapping("mechanical")
    public AjaxResult excelMechanicalAdd(@RequestBody CusImpVo vo)
    {
        try {
            Map<String, Integer> stringIntegerMap = cusImportService.insertCusMechanicalImport(vo);
            return AjaxResult.success(stringIntegerMap);
        } catch (ExcelAnalysisException e) {
            return AjaxResult.error("失败的excel解析!");
        }
    }


    /**
     * 新增 材料章节 / 材料费用项  从标准费用项复制
     */
    @PreAuthorize("@ss.hasPermi('cus:mechanicalItem:useSta')")
    @Log(title = "数据导入", businessType = BusinessType.INSERT)
    @PostMapping("mechanicalCopySta")
    public AjaxResult mechanicalCopySta()
    {
        return toAjax(cusImportService.mechanicalCopySta());
    }


    @Log(title = "数据导入", businessType = BusinessType.INSERT)
    @PostMapping("/uploadFile")
    public AjaxResult uploadFile(MultipartFile file)
    {
        String fileId = cusImportService.disposeFile(file);
        return AjaxResult.success("上传成功!",fileId);
    }

    @Log(title = "Excel导入", businessType = BusinessType.INSERT)
    @PostMapping("/uploadExcel")
    public AjaxResult uploadExcel(MultipartFile file)
    {
        String fileId = cusImportService.uploadExcel(file);
        return AjaxResult.success("上传成功!",fileId);
    }


    @Log(title = "数据导入-删除无用数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/delFile/{id}")
    public AjaxResult delFile(@PathVariable String id)
    {
        return toAjax(cusImportService.delFile(id));
    }

    @PreAuthorize("@ss.hasPermi('cus:import:download')")
    @PostMapping(value = "/upload")
    public void upload(String id, HttpServletResponse response) {
        CusImport imp = cusImportService.getById(id);
        String cuspath = SqliteUpPathConfig.getCuspath();
        String filePath = imp.getFilePath();
        File file = new File(cuspath,filePath);
        downloadFile(FileUtil.getInputStream(file),response);
    }

    /**
     * 新增CusImport
     */
    @PreAuthorize("@ss.hasPermi('cus:import:add')")
    @Log(title = "数据导入", businessType = BusinessType.INSERT)
    @PostMapping("/toLead")
    public TableDataInfo toLead(@RequestBody CusImpVo vo)
    {
        return getDataTable(cusImportService.toLead(vo));
    }

    @Log(title = "数据导入对比差异", businessType = BusinessType.INSERT)
    @PostMapping("/countDifference")
    public TableDataInfo countDifference(@RequestBody List<CusAllVo> cusAllVoList)
    {
        return getDataTable(cusImportService.countDifference(cusAllVoList));
    }

    @Log(title = "数据导入", businessType = BusinessType.INSERT)
    @PostMapping("/toCover")
    public AjaxResult toCover(@RequestBody CusImpVo vo)
    {
        return AjaxResult.success(cusImportService.toCover(vo));
    }
}
